package gitee.derumin.lib_for_derumin.mixin;

import com.google.gson.JsonElement;
import com.google.gson.JsonSyntaxException;
import net.minecraft.util.JsonHelper;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;

import static net.minecraft.util.JsonHelper.getType;

@Mixin(JsonHelper.class)
public class JsonHelperMixin {
    /**
     * @author derumin
     * @reason Unable to locate method mapping for @At(INVOKE.<target>) 'Lcom/google/gson/JsonPrimitive;isNumber()Z'
     */
    @Overwrite
    public static char asChar(JsonElement element, String name) {
        if (element.isJsonPrimitive() && element.getAsJsonPrimitive().isString()) {
            String s = element.getAsString();
            if (s.isEmpty()) {
                throw new UnsupportedOperationException("Expected " + name + " to be a Character, was empty");
            } else {
                return s.charAt(0);
            }
        } else {
            throw new JsonSyntaxException("Expected " + name + " to be a Character, was " + getType(element));
        }
    }
}
